package com.zx.zxapiinterface.controller;

import com.google.gson.Gson;
import com.zx.zxapiinterface.utils.RequireAllControllerMethodsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 张旭
 * @version 1.0
 */
@RestController
public class InterfaceController {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private RequireAllControllerMethodsUtils utils;

    @RequestMapping("/interface")
    public String InterfaceRedirect(HttpServletRequest request) {
        Map<String, String> headers = getHeaders(request);

        //1、获取当前请求路径中的类名和方法
        Map<String, String> hashmap = utils.hashmap;
        String url = headers.get("url");
        String key = "[" + url + "]";
        String res = hashmap.get(key);
        if (res == null) {
            return null;
        }
        String[] split = res.split("-");
        Object body = null;

        try {
            //通过反射构造
            Class<?> forName = Class.forName(split[0]);

            Method[] methods = forName.getMethods();
            for (Method method : methods) {
                if (method.getName().equals(split[1])) {
                    //获取当前方法的参数类型
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    if (parameterTypes.length == 0) {
                        body = method.invoke(context.getBean(forName));
                    } else {
                        Gson gson = new Gson();
                        Object param = gson.fromJson(headers.get("body"), parameterTypes[0]);
                        body = method.invoke(context.getBean(forName), param);
                    }
                }
            }
           /*
               执行这段逻辑会报错java.lang.NoSuchMethodException
               表示找不到方法参数为Object类型的方法
           //由于是object对象，所以实例化对象需要从容器中拿到
            Method classMethod = forName.getMethod(split[1], Object.class);
            //调用方法
            body = classMethod.invoke(context.getBean(forName), headers.get("body"));*/
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return String.valueOf(body);
    }

    /**
     * 获取请求头中的信息
     *
     * @param request
     * @return
     */
    public Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", request.getHeader("userId"));
        map.put("appId", request.getHeader("appId"));
        map.put("userAccount", request.getHeader("userAccount"));

        map.put("accessKey", request.getHeader("accessKey"));
        map.put("body", request.getHeader("body"));
        map.put("timestamp", request.getHeader("timestamp"));
        map.put("interfaceId", request.getHeader("interfaceId"));
        map.put("url", request.getHeader("url"));
        return map;
    }

}
